﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;

namespace umbraco.DataServices.Query
{
    /// <summary>
    /// Implementation
    /// </summary>
    public class UmbracoQuery<T> : IOrderedQueryable<T>
    {
        private readonly Expression _queryExpression;
        private readonly UmbracoQueryProvider _queryProvider;

        internal UmbracoQuery(UmbracoQueryProvider queryProvider, Expression queryExpression) {
            if (queryProvider == null)
                throw new ArgumentNullException("queryProvider");
            if (queryExpression == null)
                throw new ArgumentNullException("queryExpression");
            _queryProvider = queryProvider;
            _queryExpression = queryExpression;
        } 

        public IEnumerator<T> GetEnumerator()
        {
            return _queryProvider.ExecuteQuery<T>(_queryExpression);
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            return GetEnumerator();
        }

        public Expression Expression
        {
            get { return _queryExpression; }
        }

        public Type ElementType
        {
            get { return typeof(T); }
        }

        public IQueryProvider Provider
        {
            get { return _queryProvider; }
        }
    }
}
